from flask import current_app, jsonify, redirect, render_template, request, url_for
from flask_login import current_user, login_required
from sqlalchemy import true
from app.api.API_use import call_number, cancel_charge, change_charging_capacity, change_charging_type, get_current_state, get_orders, get_queue_size, start_charge, stop_charge
from app.role_auth import user_required
from app.user import bp


@bp.route('/<username>')
@login_required
@user_required
def index(username):
    if username != current_user.user_name:
        return render_template('user/405.html', current_user=current_user)
    current_app.logger.info('user: %s access Home page', username)
    title = username + '\'s Homepage'
    return render_template('user/index.html', current_user=current_user, title=title)


@bp.route('/<username>/state')
@login_required
@user_required
def get_user_state(username):
    ret = get_current_state(username)
    current_app.logger.info('user: %s get current state: %s', username, ret)
    return jsonify(ret)


@bp.route('/<username>/order')
@login_required
@user_required
def order(username):
    if username != current_user.user_name:
        return render_template('user/405.html', current_user=current_user)
    current_app.logger.info('user: %s check orders', username)
    title = username + '\'s Orders'
    return render_template('user/order.html', orders=get_orders(username), title=title)


@bp.route('/<username>/queue')
@login_required
@user_required
def update_queue(username):
    current_app.logger.info('user: %s gets queue', username)
    return jsonify(get_queue_size(username))


@bp.route('/<username>/callnumber', methods=['POST'])
@login_required
@user_required
def get_queue_number(username):
    json = request.get_json()
    ret = call_number(username, json['charge_mode'], int(
        json['charge_quantity']), int(json['total_quantity']))
    if ret['state'] == 5:
        ret = get_current_state(username)
    current_app.logger.info('user: %s call number: %s', username, ret)
    return jsonify(ret)


@bp.route('/<username>/change', methods=['POST'])
@login_required
@user_required
def change_request(username):
    json = request.get_json()
    ret = []
    if (json['mode_changed']):
        ret = change_charging_type(username, json['charge_mode'])
        current_app.logger.info(
            'user: %s change charge mode to %s\n     New state: %s', username, json['charge_mode'], ret)
    change_charging_capacity(username, int(json['charge_quantity']))
    current_app.logger.info(
        'user: %s change charge capacity to %s', username, json['charge_quantity'])
    return jsonify(ret)


@bp.route('/<username>/confirm')
@login_required
@user_required
def confirm_charge(username):
    start_charge(username)
    current_app.logger.info('user: %s start charge', username)
    return jsonify(success=True)


@bp.route('/<username>/cancel')
@login_required
@user_required
def cancel_charge_request(username):
    cancel_charge(username)
    current_app.logger.info('user: %s cancel charge', username)
    return jsonify(success=True)


@bp.route('/<username>/stopcharge')
@login_required
@user_required
def stop_charging(username):
    stop_charge(username, 'user')
    current_app.logger.info('user: %s stop charge', username)
    return jsonify(success=True)


@bp.route('/<username>/remaintime')
@login_required
@user_required
def get_charge_remaintime(username):
    ret = get_current_state(username)
    current_app.logger.info(
        'user: %s get charge remain time [%s]', username, ret['remaining_time'])
    return jsonify(remaining_time=ret['remaining_time'])
